package com.myteam.fugaocrm.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.myteam.fugaocrm.modle.FgProjectPafrolRecord;
import com.myteam.fugaocrm.modle.FgProjectRepairRecode;
import com.myteam.fugaocrm.modle.FgStaff;
import com.myteam.fugaocrm.service.StaffServiceImpl;
import com.myteam.fugaocrm.sqldb.mapper.FgProjectPafrolRecordMapper;
import com.myteam.fugaocrm.sqldb.mapper.FgProjectRepairRecodeMapper;

@RequestMapping("projectRepaird")
@Transactional
@Controller
public class ProjectRepairdController extends BaseController {

    @Resource
    protected FgProjectPafrolRecordMapper fgProjectPafrolRecordMapper;
    
    @Resource
    protected FgProjectRepairRecodeMapper fgProjectRepairRecodeMapper;
    
    @Resource 
    protected StaffServiceImpl staffServiceImpl;
    
    @RequestMapping("toPage")
    public ModelAndView toPage() {
    	String projectId = request.getParameter("projectId");
        Map<String, Object> attrs = new HashMap<>();
        List<FgProjectPafrolRecord> projectPafrolRecordList = new ArrayList<FgProjectPafrolRecord>();
        List<FgProjectRepairRecode> projectRepairRecodeList = new ArrayList<FgProjectRepairRecode>();
        if(StringUtils.isNotBlank(projectId)){
        	projectPafrolRecordList = fgProjectPafrolRecordMapper.selectAll(Integer.parseInt(projectId));
        	projectRepairRecodeList = fgProjectRepairRecodeMapper.selectAll(Integer.parseInt(projectId));
        	
        }
        attrs.put("projectId", projectId);
        attrs.put("projectPafrolRecordList", projectPafrolRecordList);
        attrs.put("projectRepairRecodeList", projectRepairRecodeList);
        getParmMap(attrs);
        return new ModelAndView("project_repaird.jsp", attrs);
    }
    
    @RequestMapping("doSave")
    public ModelAndView doSave(
        String pafrolUpdateListJson, String pafrolInsertListJson, 
        String repairdUpdateListJson,String repairdInsertListJson
    ) {
    	String projectId = request.getParameter("projectId");
    	if(StringUtils.isBlank(projectId)){
    		//TODO 提示页面错误信息
    		logger.error("项目id不能为空");
    	}
    	String staffName = "";
		if(null != request.getSession() && null != request.getSession().getAttribute("staffName")){
			staffName = request.getSession().getAttribute("staffName").toString();
		}
        List<FgProjectPafrolRecord> pafrolUpdateList = JSON.parseArray(pafrolUpdateListJson, FgProjectPafrolRecord.class);
        List<FgProjectPafrolRecord> pafrolInsertList = JSON.parseArray(pafrolInsertListJson, FgProjectPafrolRecord.class);
        List<FgProjectRepairRecode> repairdUpdateList = JSON.parseArray(repairdUpdateListJson, FgProjectRepairRecode.class);
        List<FgProjectRepairRecode> repairdInsertList = JSON.parseArray(repairdInsertListJson, FgProjectRepairRecode.class);
        for (FgProjectPafrolRecord i : pafrolUpdateList) {
        	i.setModifiedUser(staffName);
        	i.setModifiedDate(new Date());
            fgProjectPafrolRecordMapper.updateOne(i);
        }
        for (FgProjectPafrolRecord i : pafrolInsertList) {
        	i.setProjectId(Integer.parseInt(projectId));
        	i.setCreatedUser(staffName);
        	i.setCreatedDate(new Date());
        	i.setIsDelete(0);
            fgProjectPafrolRecordMapper.insertOne(i);
        }
        for (FgProjectRepairRecode i : repairdUpdateList) {
        	i.setModifiedUser(staffName);
        	i.setModifiedDate(new Date());
            fgProjectRepairRecodeMapper.updateOne(i);
        }
        for (FgProjectRepairRecode i : repairdInsertList) {
        	i.setProjectId(Integer.parseInt(projectId));
        	i.setCreatedUser(staffName);
        	i.setCreatedDate(new Date());
        	i.setIsDelete(0);
            fgProjectRepairRecodeMapper.insertOne(i);
        }
        return new ModelAndView("redirect:toPage?projectId="+projectId);
    }
    
    private void getParmMap(Map<String, Object> attrs) {
        //查询项目负责人
        List<FgStaff> staffList = new ArrayList<FgStaff>();
        staffList = staffServiceImpl.all();
        attrs.put("staffList", staffList);
    }
}
